from housecentapp.models import Tags, Cities, Houses


def rtag(cityname):
    city = Cities.objects.get(name=cityname)
    houses = Houses.objects.filter(city=city)
    tags=[]
    for house in houses:
        mtags=Tags.objects.filter(houses=house)
        for tag in mtags:
            if tag not in tags:
                tags.append(tag)
    rooms = {}
    beds = {}
    peoples = {}
    livings = {}
    for tag in tags:
        if "床" in tag.name or "榻榻米" in tag.name or "沙发" in tag.name:
            beds.update({tag.id: {
                "id": tag.id,
                "name": tag.name
            }})
        elif "室" in tag.name:
            rooms.update({tag.id: {
                "id": tag.id,
                "name": tag.name
            }})
        elif "人" in tag.name:
            peoples.update({tag.id: {
                "id": tag.id,
                "name": tag.name
            }})
        else:
            livings.update({tag.id: {
                "id": tag.id,
                "name": tag.name
            }})
    newrooms = {}
    for i in range(1, 7):
        for j in range(0, 6):
            for room in rooms:
                if rooms[room]["name"][0] == str(i) and rooms[room]["name"][2] == str(j):
                    newrooms.update({rooms[room]["id"]: {
                        "id": rooms[room]["id"],
                        "name": rooms[room]["name"],
                    }})
    newbeds = {}
    for i in range(1, 10):
        for room in beds:
            if beds[room]["name"][0] == str(i):
                newbeds.update({beds[room]["id"]: {
                    "id": beds[room]["id"],
                    "name": beds[room]["name"],
                }})
    newpeoples = {}
    import re
    for i in range(1, 11):
        for people in peoples:
            a = re.findall("[\d]*", peoples[people]["name"])
            if a[2] == str(i):
                newpeoples.update({people: {
                    "id": people,
                    "name": peoples[people]["name"],
                }})
    return {"房间配置":newrooms,"床数量":newbeds,"可住人数":newpeoples,"出租方式":livings}